package com.chima.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.chima.dao.PostDao;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Post;
import com.chima.service.PostService;

public class PostServiceImpl implements PostService {
	private  PostDao postDao;
	
	public PostDao getPostDao() {
		return postDao;
	}

	public void setPostDao(PostDao postDao) {
		this.postDao = postDao;
	}

	public PageResponseBean findPost(PageRequestBean pageRequestBean) {
		
		int firstResult =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit();
//		int maxResults =(pageRequestBean.getPage()-1)*pageRequestBean.getLimit()+pageRequestBean.getLimit();
		int maxResults =pageRequestBean.getLimit();
		int total =postDao.findByTotalCount(pageRequestBean.getDept());
		// 获取分页查询条件
//		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Post.class);
		DetachedCriteria detachedCriteria = pageRequestBean.getCriteria();// 不一定存在
		List<Post> posts= postDao.findByCriteria(detachedCriteria, firstResult, maxResults);
		PageResponseBean pageResponseBean = new PageResponseBean();
		pageResponseBean.setRows(posts);
		pageResponseBean.setTotal(total);
		return pageResponseBean;
	}

	public void savePost(Post post) {
		postDao.save(post);
	}

	public void updatePost(Post post) {
		postDao.update(post);
		
	}

	public void deletePost(int id) {
		postDao.delete(id);
		
	}

	public List<Post> findPostByDept(int depart) {
		
		return postDao.findPostByDept(depart);
	}
	
}
